import api from "../api/api.js";
import {setLocalData,getLocalData,removeLocalData} from "./utils.js";

//获取类别信息
export const  getMajors = async () =>{
	let majorList = getLocalData("majorList");
	if(majorList &&　majorList.length > 0){
		return majorList;
	}
	let ret = await api.getMajors();
	if (ret) {
		majorList =  ret.data.list;
		setLocalData("majorList",majorList);
	}else{
		majorList = [];
	}
	return majorList;
}

//根据类别id获取科目信息 全部科目
export const getSubjects = async (majorId) =>{
	if(!majorId && majorId !== 0){
		return [];
	}
	let ret = await api.getSubjectList({majorId: majorId});
	if(ret){
		return ret.data.list;
	}
	return [];
}
//根据类别id获取科目信息 无公共科目
export const getSubjectsNoCommon = async (majorId) =>{
	if(!majorId && majorId !== 0){
		return [];
	}
	let ret = await api.getKzSubjects({majorId: majorId});
	if(ret){
		return ret.data.list;
	}
	return [];
}

//根据科目id 获取知识点
export const getKwpts = async(subjectId)=>{
	if(!subjectId && subjectId !== 0){
		return [];
	}
	let ret = await api.getKwpts({subjectId: subjectId});
	if(ret){
		return ret.data.list;
	}
	return [];
}

//根据标签类别 获取标签
export const getTags = async(type)=>{
	if(!type && type !== 0){
		return [];
	}
	let ret = await api.getTags({type: type});
	if(ret){
		return ret.data.list;
	}
	return [];
}

//科目id 获取章节列表
export const getChapters = async(subjectId)=>{
	if(!subjectId && subjectId !== 0){
		return [];
	}
	let ret = await api.getChapters({subjectId: subjectId});
	if(ret){
		return ret.data.list;
	}
	return [];
}


//获取全部学院信息
export const getColleges = async ()=>{
	let collegeList = getLocalData("collegeList");
	if(collegeList &&　collegeList.length > 0){
		return collegeList;
	}
	let ret = await api.getColleges();
	if (ret) {
		collegeList =  ret.data.list;
		setLocalData("collegeList",collegeList);
	}else{
		collegeList = [];
	}
	return collegeList;
}

export const removeCollegesCache = ()=>{
	removeLocalData("collegeList");
}

//根据学院id 获取所有专业列表
export const getSpecialities = async (collegeId) =>{
	if(!collegeId && collegeId !== 0){
		return [];
	}
	let ret = await api.getSpecialtys({collegeId: collegeId});
	if(ret){
		return ret.data.list;
	}
	return [];
}

//获取年级系列表
export const  getGrades = async ()=>{
	let gradeList = getLocalData("gradeList");
	if(gradeList &&　gradeList.length > 0){
		return gradeList;
	}
	let ret = await api.getGrades();
	if (ret) {
		gradeList =  ret.data.list;
		setLocalData("gradeList",gradeList);
	}else{
		gradeList = [];
	}
	return gradeList;
}
export const removeGradesCache = ()=>{
	removeLocalData("gradeList");
}


//根据专业id 和 年级 获取班级列表
export const getClasss = async (specialityId,gradeName) =>{
	if(!specialityId && specialityId !== 0){
		return [];
	}
	let ret = await api.getClasss({specialityId: specialityId,gradeName:gradeName});
	if(ret){
		return ret.data.list;
	}
	return [];
}

//获取导入模板的连接
export const getExcelImportTemplateUrl = async(typeName)=>{
	let url = getLocalData(typeName);
	if(!url){
		let apiName;
		let param = {};
		switch (typeName){
			case "teacherTemp":
				apiName = "downTemplate";
				param.templateType = "111";
				break;
			case "studentTemp":
				apiName = "downTemplate";
				param.templateType = "121";
				break;
			case "knowledgeTemp":
				//知识点
				apiName = "downTemplate";
				param.templateType = "131";
				break;
			case "collegeTemp":
				//学院
				apiName = "downTemplate";
				param.templateType = "141";
				break;
			case "specialtyTemp":
				//专业
				apiName = "downTemplate";
				param.templateType = "151";
				break;
			case "classTemp":
				//班级
				apiName = "downTemplate";
				param.templateType = "161";
				break;
			case "questionTemp":
				//题库
				apiName = "downQuestionTemplate";
				param.templateType = "211";
				break;
			case "paperTemp":
				//试卷
				apiName = "downQuestionTemplate";
				param.templateType = "221";
				break;
			case "labelTemp":
				param.templateType = "231";
				apiName = "downQuestionTemplate"
				break;
			case "chapterTemp":
				param.templateType = "241";
				apiName = "downQuestionTemplate"
				break;
		}
		let ret = await api[apiName](param);
		if(ret){
			url = ret.data.url;
			setLocalData(typeName,url);
		}
	}
	return url;
}

export const downloadFileByUrl =(templateUrl,fileName)=>{
	const ele = document.createElement('a');
	ele.setAttribute('href', templateUrl); //设置下载文件的url地址
	ele.setAttribute('download',fileName);//用于设置下载文件的文件名
	ele.click();
}
